using B.S.ReportEvent.ApiNew.Commands.Instance;
using B.S.ReportEvent.ApiNew.Dtos.ResponseDto;
using B.S.ReportEvent.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using B.S.ReportEvent.Infrastructure.Interfaces;
using B.S.ReportEvent.Domain.Entities;
using Microsoft.AspNetCore.Authorization;

namespace B.S.ReportEvent.ApiNew.Controllers
{
    /// <summary>
    /// 事件实例控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class InstanceController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<InstanceController> _logger;

        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="mediator"></param>
        public InstanceController(ILogger<InstanceController> logger, IMediator mediator)
        {
            _logger = logger;
            _mediator = mediator;
        }

        /// <summary>
        /// 新增事件实例
        /// </summary>
        [HttpPost("CreateInstance")]
        public async Task<ApiResult> CreateInstance([FromBody] CreateInstanceCommand param)
        {
            var result = await _mediator.Send(param);
            return result;
        }

        /// <summary>
        /// 查询当前登录用户的所有待办实例
        /// </summary>
        [HttpGet("MyTodoInstances")]
        public async Task<ApiResult<GetMyTodoInstancesResponseDto>> GetMyTodoInstances()
        {
            // 假设用户ID存储在 ClaimTypes.NameIdentifier
            var userIdStr = User.Claims.First(x => x.Type == "UserId");

            var result = await _mediator.Send(new GetMyTodoInstancesCommand(userIdStr.Value));
            return result;
        }
    }
}
